/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelProblema.java
 *
 * Created on May 4, 2011, 7:02:42 AM
 */
package View;

import Controller.*;
import Model.Celdam;
import Model.DatosPotencia;
import Model.twoWays;
import View.Celda;
import com.sun.jmx.snmp.tasks.Task;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Event;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
import sun.misc.Sort;

/**
 *
 * @author tuareg
 */
public class PanelProblema extends javax.swing.JPanel {
    public int actual;
    Celdam[][] total;
    
    /** Creates new form PanelProblema */
    public PanelProblema(int bloques, int tratamientos, String tipo) {
        initComponents();
        ArrayList matriz[][][];
        if (tipo.equals("Manual")) {
            initComponents();
            panelProblema.setLayout(new GridLayout(bloques, tratamientos));
            for (int i = 0; i < bloques; i++) {
                final int x=i;
                ArrayList<JButton> restriccion = new ArrayList<JButton>();
                for (int j = 0; j < tratamientos; j++) {
                    final int y=j;
                    JPanel campo = new JPanel(new GridLayout(1, 2));
                    JLabel label = new JLabel(" ");
                    final JButton boton = new JButton();
                    boton.setText("");                  
                    boton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Edit.png")));
                    boton.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            Celda cel = new Celda();
//                            Celdam celm = new Celdam();
                            Celdam.setPosX(x);
                            Celdam.setPosY(y);
                            Celdam[][] matriz;
                            matriz = twoWays.getMatriz();
                            total = matriz;
                            
                            if(matriz!=null && matriz[x][y]!=null){
//                                celm = matriz[x][y];
                                boton.setText("");
                                cel.Inicializar(String.valueOf(matriz[x][y].getDatos()), String.valueOf(matriz[x][y].getLambda1())
                                            , String.valueOf(matriz[x][y].getLambda2()), String.valueOf(matriz[x][y].getLambda3())
                                            , String.valueOf(matriz[x][y].getLambda4()));
                            }
                            cel.setVisible(true);
                            
                        }
                    });
                    
                    campo.add(boton);
                    campo.add(label);
                    restriccion.add(boton);
                    panelProblema.add(campo);
                    datosPorCelda.add(boton);
                    celda.add(new Celda());
                }
                problema.add(restriccion);
            }
            
        } else {
            MainController.generarMatrizAleatoria(bloques, tratamientos);
        }

    }

    public void mostrarInformacion(int posX,int posY){
        
    }
    
    public ArrayList<Double> Ks = new ArrayList<Double>();
    public ArrayList<Double> j1 = new ArrayList<Double>();
    public ArrayList<Double> j2 = new ArrayList<Double>();
    public ArrayList<Double> j3 = new ArrayList<Double>();
    public ArrayList<Double> j4 = new ArrayList<Double>();
    public ArrayList<Double> j5 = new ArrayList<Double>();
    
    String e ="";
    private void botonResolverActionPerformed(java.awt.event.ActionEvent evt){
        if(twoWays.getEstadisticaACalcular().equals("K")){
        for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        e = "Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado();
        System.out.println(e);
        Archivos.escrituraArchivos(e);
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                e = "Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4();
                System.out.println(e);
                Archivos.escrituraArchivos(e);
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.Kgeneralizada();
        Ks.add(KGeneralizada);
        e = "La K Generalizada linda es: " + KGeneralizada;
        System.out.println(e);
        Archivos.escrituraArchivos(e);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(Ks);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){
            e = "Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i);
            System.out.println(e);
            Archivos.escrituraArchivos(e);
        }
        }
        else if(twoWays.getEstadisticaACalcular().equals("J1")){
        for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        e = "Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado();
        System.out.println(e);
        Archivos.escrituraArchivos(e);
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                System.out.println("Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4());
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.j1();
        j1.add(KGeneralizada);
        e = "La J1 linda es: " + KGeneralizada;
        System.out.println(e);
        Archivos.escrituraArchivos(e);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(j1);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){
            e ="Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i);
            System.out.println(e);
            Archivos.escrituraArchivos(e);
        }
        }
        else if(twoWays.getEstadisticaACalcular().equals("J2")){        for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        System.out.println("Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado());
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                System.out.println("Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4());
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.Kgeneralizada();
        j2.add(KGeneralizada);
        System.out.println("La K Generalizada linda es: " + KGeneralizada);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(j2);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){            
            System.out.println("Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i));}}
        else if(twoWays.getEstadisticaACalcular().equals("J3")){        for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        System.out.println("Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado());
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                System.out.println("Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4());
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.Kgeneralizada();
        j3.add(KGeneralizada);
        System.out.println("La K Generalizada linda es: " + KGeneralizada);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(j3);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){
            System.out.println("Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i));}}
        else if(twoWays.getEstadisticaACalcular().equals("J4")){        for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        System.out.println("Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado());
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                System.out.println("Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4());
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.Kgeneralizada();
        j4.add(KGeneralizada);
        System.out.println("La K Generalizada linda es: " + KGeneralizada);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(j4);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){
            System.out.println("Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i));}}
        else if(twoWays.getEstadisticaACalcular().equals("J5")){
                for (int x = 0; x<twoWays.getmPiloto() ; x++){
        MatrizDatos matriz = new MatrizDatos();
        matriz.ordenamientoSecuencia();
        System.out.println("Varianza: "+twoWays.getVarianza()+" Error: "+ twoWays.getError()+" Significancia: "+twoWays.getSignificancia()+ " M estimado: "+ twoWays.getmEstimado());
        int filas = twoWays.getMatriz().length;
        int columnas = twoWays.getMatriz()[0].length;
        for (int i = 0; i < filas; i++) {
            for (int j = 0; j < columnas; j++) {
                System.out.println("Número de datos: "+ twoWays.getMatriz()[i][j].getDatos() + " Fila: "+i+" Columna: "+j+ " lambda1: "+twoWays.getMatriz()[i][j].getLambda1()+" lambda2: "+twoWays.getMatriz()[i][j].getLambda2()+" lambda3: "+twoWays.getMatriz()[i][j].getLambda3()+" lambda4: "+twoWays.getMatriz()[i][j].getLambda4());
                for (int k = 0; k < twoWays.getMatriz()[i][j].getDatos(); k++) {
                    System.out.println(twoWays.getMatriz()[i][j].getValores().get(k));
                    /*if(twoWays.getMatriz()[i][j].getOrdenamiento().get(k) == null){
                        System.out.println("Estoy Vacío");
                    }*/
                    
                }
                System.out.println("-----------------------");
            }
        }
        double KGeneralizada = 0;
        //MainController.simular();
        Calculadora c = new Calculadora();
        KGeneralizada = c.Kgeneralizada();
        j5.add(KGeneralizada);
        System.out.println("La K Generalizada linda es: " + KGeneralizada);
//            System.out.println("La K askldjaskldjasd: " + c.estadisticaFriedman(5,filas,columnas));
        KGeneralizada = 0;            
        MainController.reRegenerarDatos();}
        MainController.generarLambdas(j5);
        PotenciaControl.calcularPotencia(MainController.puntoCritico, twoWays.getmEstimado());
        for(int i = 0 ; i < DatosPotencia.getValoresDeltaTao().size();i++){
            System.out.println("Tao = "+ DatosPotencia.getValoresDeltaTao().get(i) +" Potencia = " + DatosPotencia.getValoresPotencia().get(i));}}
        
    }    
    
    private void botonGeneralizarActionPerformed(java.awt.event.ActionEvent evt) {
        ConfigLambda configLambda = new ConfigLambda();
        configLambda.setVisible(true);
    } 
    
    private void botonTaosActionPerformed(java.awt.event.ActionEvent evt) {
        VentanaIngresoTaos ventanaTaos = new VentanaIngresoTaos();
        ventanaTaos.setVisible(true);
    } 
    
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        panelProblema = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        botonResolver = new javax.swing.JButton();
        botonGeneralizar = new javax.swing.JButton();
        botonTaos = new javax.swing.JButton();
        
        javax.swing.GroupLayout panelProblemaLayout = new javax.swing.GroupLayout(panelProblema);
        panelProblema.setLayout(panelProblemaLayout);
        panelProblemaLayout.setHorizontalGroup(
                panelProblemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 398, Short.MAX_VALUE));
        panelProblemaLayout.setVerticalGroup(
                panelProblemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 261, Short.MAX_VALUE));

        jScrollPane1.setViewportView(panelProblema);

        botonResolver.setText("Resolver");
        botonResolver.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Forward.png")));
        botonResolver.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonResolverActionPerformed(evt);
            }
        });

        botonGeneralizar.setText("Generalizar");
        botonGeneralizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Template.png")));
        botonGeneralizar.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGeneralizarActionPerformed(evt);
            }
        });
        
        botonTaos.setText("Taos y Betas");
        botonTaos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/Stats.png")));
        botonTaos.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonTaosActionPerformed(evt);
            }
        });
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(botonResolver).addComponent(botonGeneralizar).addComponent(botonTaos).addContainerGap(302, Short.MAX_VALUE)));
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(botonResolver).addComponent(botonGeneralizar).addComponent(botonTaos).addContainerGap(21, Short.MAX_VALUE)));

       

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE).addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap()));
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public ArrayList<JButton> datosPorCelda = new ArrayList<JButton>();
    public ArrayList<Celda> celda = new ArrayList<Celda>();
    private javax.swing.JButton botonResolver;
    private javax.swing.JButton botonGeneralizar;
    private javax.swing.JButton botonTaos;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelProblema;
    // End of variables declaration//GEN-END:variables
    ArrayList<ArrayList<JButton>> problema = new ArrayList<ArrayList<JButton>>();
    ArrayList<JTextField> vectorRecursos = new ArrayList<JTextField>();
    ArrayList<JTextField> vectorC = new ArrayList<JTextField>();
    ArrayList<JComboBox> vectorTipoRestriccion = new ArrayList<JComboBox>();
}
